package com.jn.zhinongbao.adapter.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.supportingagriculturaltreasures.R;
import com.example.supportingagriculturaltreasures.bean.Cart;

import java.util.List;

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.CartViewHolder> {

    private Context context;
    private List<Cart> cartList;
    private OnCartItemClickListener listener;

    public interface OnCartItemClickListener {
        void onQuantityChanged(Cart cart, int newQuantity);
        void onItemClick(Cart cart);
        void onSelectionChanged(Cart cart, boolean isSelected);
    }

    public CartAdapter(Context context, List<Cart> cartList, OnCartItemClickListener listener) {
        this.context = context;
        this.cartList = cartList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_cart, parent, false);
        return new CartViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CartViewHolder holder, int position) {
        Cart cart = cartList.get(position);
        
        holder.tvProductName.setText(cart.getProductName());
        holder.tvProductPrice.setText("¥" + String.format("%.2f", cart.getProductPrice()));
        holder.tvQuantity.setText(String.valueOf(cart.getQuantity()));
        holder.tvTotalPrice.setText("¥" + String.format("%.2f", cart.getProductPrice() * cart.getQuantity()));
        
        // 设置选择状态
        holder.cbSelected.setChecked(cart.isSelected());
        
        // 使用Glide加载商品图片
        loadProductImage(holder.ivProductImage, cart.getProductImageUrl());

        // 选择框点击事件
        holder.cbSelected.setOnCheckedChangeListener((buttonView, isChecked) -> {
            cart.setSelected(isChecked);
            if (listener != null) {
                listener.onSelectionChanged(cart, isChecked);
            }
        });

        // 减少数量按钮
        holder.btnDecrease.setOnClickListener(v -> {
            int currentQuantity = cart.getQuantity();
            if (currentQuantity > 0) {
                if (listener != null) {
                    listener.onQuantityChanged(cart, currentQuantity - 1);
                }
            }
        });

        // 增加数量按钮
        holder.btnIncrease.setOnClickListener(v -> {
            int currentQuantity = cart.getQuantity();
            if (listener != null) {
                listener.onQuantityChanged(cart, currentQuantity + 1);
            }
        });

        // 删除按钮
        holder.btnDelete.setOnClickListener(v -> {
            if (listener != null) {
                listener.onQuantityChanged(cart, 0); // 数量设为0表示删除
            }
        });

        // 整个item点击
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(cart);
            }
        });
    }

    @Override
    public int getItemCount() {
        return cartList.size();
    }

    /**
     * 使用Glide加载商品图片
     */
    private void loadProductImage(ImageView imageView, String imageUrl) {
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.ic_launcher_foreground) // 加载中显示的图片
                .error(R.drawable.ic_launcher_foreground) // 加载失败显示的图片
                .transform(new RoundedCorners(16)); // 圆角处理

        if (imageUrl != null && !imageUrl.isEmpty()) {
            Glide.with(context)
                    .load(imageUrl)
                    .apply(options)
                    .into(imageView);
        } else {
            // 如果没有图片URL，显示默认图片
            imageView.setImageResource(R.drawable.ic_launcher_foreground);
        }
    }

    static class CartViewHolder extends RecyclerView.ViewHolder {
        CheckBox cbSelected;
        ImageView ivProductImage;
        TextView tvProductName;
        TextView tvProductPrice;
        TextView tvQuantity;
        TextView tvTotalPrice;
        ImageButton btnDecrease;
        ImageButton btnIncrease;
        ImageButton btnDelete;

        public CartViewHolder(@NonNull View itemView) {
            super(itemView);
            cbSelected = itemView.findViewById(R.id.cb_selected);
            ivProductImage = itemView.findViewById(R.id.iv_product_image);
            tvProductName = itemView.findViewById(R.id.tv_product_name);
            tvProductPrice = itemView.findViewById(R.id.tv_product_price);
            tvQuantity = itemView.findViewById(R.id.tv_quantity);
            tvTotalPrice = itemView.findViewById(R.id.tv_total_price);
            btnDecrease = itemView.findViewById(R.id.btn_decrease);
            btnIncrease = itemView.findViewById(R.id.btn_increase);
            btnDelete = itemView.findViewById(R.id.btn_delete);
        }
    }
} 